package com.smec.remes.module.register.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;
import com.smec.moblie.R;
import com.smec.moblie.databinding.ActivityMantrBinding;
import com.smec.remes.base.SmecBaseActivity;
import com.smec.remes.bean.AbleMatnr;
import com.smec.remes.module.register.SmecRegisterPresenter;
import com.smec.remes.module.register.adapter.SmecMantrAdapter;
import com.smec.remes.module.search.component.BottomDialog;
import com.smec.remes.module.search.component.CircleView;
import com.smec.remes.util.CommonUtils;
import com.smec.remes.util.WmsConstant;


import java.util.ArrayList;
import java.util.HashMap;

import static com.smec.remes.module.search.SmecSearchActivity.BUY_NULL;

/**
 * Created by Administrator on 2017/12/9.
 */

public class SmecMantrActivity extends SmecBaseActivity<SmecRegisterPresenter> {

    private ActivityMantrBinding activityMantrBinding;
    public static final String MANTR="MANTR";
    public static final String MANTRFAIL="MANTRFAIL";
    public static final String SelectMantr="SelectMantr";
    private ArrayList<AbleMatnr> newBuyList=new ArrayList<>();
    private Handler mHandler=new Handler();
    private SmecMantrAdapter smecMantrAdapter;
    private String  status;
    private int pageNumber=1;
    private String condition="";

    public static final String MANTR_BUY_NOT_NULL="BUY_NOT_NULL";

    //绘图
    private ViewGroup viewGroup;

    //弹窗
    BottomDialog bottomDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMantrBinding=DataBindingUtil.setContentView(this,R.layout.activity_mantr);
        smecMantrAdapter=new SmecMantrAdapter(this,mRxBus);
        activityMantrBinding.mantrRecyclerView.setLayoutManager(new LinearLayoutManager(SmecMantrActivity.this));
        activityMantrBinding.mantrRecyclerView.setAdapter(smecMantrAdapter);

        viewGroup = (ViewGroup) getWindow().getDecorView();
        bottomDialog = new BottomDialog(this, R.style.BottomDialog , mRxBus);

        _dailogClick();
        _return();
        _firstGet();
        //_submit();
    }

    public void _firstGet(){
        activityMantrBinding.ptr.setHeaderView(WmsConstant.getSinaRefreshView());
        activityMantrBinding.ptr.setBottomView(new LoadingView(getApplicationContext()));
        activityMantrBinding.ptr.setAutoLoadMore(true);
        activityMantrBinding.ptr.setEnableOverScroll(false);
        activityMantrBinding.ptr.setEnableLoadmore(true);
        activityMantrBinding.ptr.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNumber=1;
                status="onRefresh";
                mPresenter.ableMatnr(condition,pageNumber);
                pageNumber+=1;
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                status="onLoadMore";
                mPresenter.ableMatnr(condition,pageNumber);
                pageNumber+=1;
            }

        });
        activityMantrBinding.getRoot().postDelayed(new Runnable() {
            @Override
            public void run() {
                activityMantrBinding.ptr.startRefresh();
            }
        },500);

        activityMantrBinding.edSearch.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    searchByKeyWord();
                }
                return false;
            }
        });

        activityMantrBinding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchByKeyWord();
            }
        });

        activityMantrBinding.edSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                activityMantrBinding.ivDelete.setVisibility(View.VISIBLE);
                activityMantrBinding.ivSearch.setImageResource(R.drawable.smec_remes_lightsearch);
            }
        });
        activityMantrBinding.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                condition="";
                activityMantrBinding.edSearch.setText("");
            }
        });
    }

    private void searchByKeyWord(){
        condition=activityMantrBinding.edSearch.getText()!=null? activityMantrBinding.edSearch.getText().toString():"";
        CommonUtils.hideImmManager(activityMantrBinding.getRoot());
        activityMantrBinding.ptr.startRefresh();
    }

    public void _return(){
        activityMantrBinding.mantrTopbar.getTvTextLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRxBus.post("BuyList",smecMantrAdapter.getBuycarList());
                SmecMantrActivity.this.finish();
            }
        });
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(MANTR)
            }
    )

    public void _getMantr( ArrayList<AbleMatnr> arrayList){
        if(CommonUtils.notEmpty(arrayList)){
            smecMantrAdapter.getList(arrayList,status);
        }else {
            if(status.equals("onRefresh")){
                smecMantrAdapter.clearList();
            }
        }

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(status.equals("onRefresh")){
                    activityMantrBinding.ptr.finishRefreshing();
                }else {
                    activityMantrBinding.ptr.finishLoadmore();
                }
            }
        },500);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(MANTRFAIL)
            }
    )

    public void _mantrFail(String error){
        if(status.equals("onRefresh")){
            activityMantrBinding.ptr.finishRefreshing();
        }else {
            activityMantrBinding.ptr.finishLoadmore();
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SelectMantr)
            }
    )

    public void _SelectMantr(ArrayList<AbleMatnr> arrayList){
        if(CommonUtils.notEmpty(arrayList)){
            String num= String.valueOf(arrayList.size());
            smecMantrAdapter._setBuyList(arrayList);
            activityMantrBinding.cartNum.setVisibility(View.VISIBLE);
            activityMantrBinding.cartNum.setText(num);
        }
    }

    @Override
    public SmecRegisterPresenter getPresenter() {
        return new SmecRegisterPresenter(this);
    }

    /**
     * 显示popuWindow
     */
    public void _dailogClick(){
        activityMantrBinding.floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _showPopuWindow(v);
            }
        });

        activityMantrBinding.cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                _showPopuWindow(v);
            }
        });
    }

    /**
     * 显示悬浮框
     */
    private void _showPopuWindow(View view){
        _loadData();
        if(CommonUtils.notEmpty(bottomDialog.getArrayList())){
            bottomDialog.show();
        }else {
            Toast.makeText(SmecMantrActivity.this,"无添加物料",Toast.LENGTH_SHORT).show();
        }
    }

    private void _loadData(){
        bottomDialog.setArrayList(smecMantrAdapter.getBuycarList());
    }


    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(BUY_NULL)
            }
    )
    public void buyNull(Object o){
        smecMantrAdapter.setBuycarList(new ArrayList<AbleMatnr>());
        activityMantrBinding.cartNum.setVisibility(View.GONE);
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(MANTR_BUY_NOT_NULL)
            }
    )
    public void buyNotNull(final HashMap<String, Object> hashMap){
        if(hashMap.size() == 3) {
            //获取起始坐标
            int[] startPoint = new int[2];
            startPoint[0] = (int) hashMap.get("x");
            startPoint[1] = (int) hashMap.get("y");

            //获取结束坐标
            int[] endPoint = new int[2];
            activityMantrBinding.cartBox.getLocationInWindow(endPoint);


            //生成View
            CircleView view = new CircleView(SmecMantrActivity.this);
            view.setCircleStartPoint(startPoint[0], startPoint[1]);
            view.setCircleEndPoint(endPoint[0] + activityMantrBinding.cartBox.getWidth() / 2 + 10, endPoint[1]);
            //添加View并执行动画
            viewGroup.addView(view);
            view.startAnimation();

            activityMantrBinding.getRoot().postDelayed(new Runnable() {
                @Override
                public void run() {
                    activityMantrBinding.cartNum.setVisibility(View.VISIBLE);
                    activityMantrBinding.cartNum.setText((String) hashMap.get("size"));
                }
            }, 600);
        }else{
            activityMantrBinding.cartNum.setVisibility(View.VISIBLE);
            activityMantrBinding.cartNum.setText((String) hashMap.get("size"));
            smecMantrAdapter.setBuycarList((ArrayList<AbleMatnr>)hashMap.get("newList"));
        }

    }
}
